There are many ways to do an image
rollover. In this example, we see that the JavaScript preloads the images, where in the
first, the images must load when the mouse passes over. This example also does a browser
check to make sure the browser supports the script.
The first thing you should do is have a browsercheck. The
best browsercheck when we're doing a rollover is if(document.images) .
This checks if the browser supports the image object. The reason we do this is because
there might come (I know Opera4 that comes later this spring will support it) browsers
other then ns and ie that supports this, and its much easier then that navigator.appname==3 .
Then next thing you should do is preload the images. First
"make" a new image object with the new Image() function myimage=new Image() . Then set the src property for the image
object. The src property works as in regular HTML. (myimage.src='myimage.gif' )
Do I have to do that for every image you ask? Since we are
very nice we have made a preloading function for you so you wont have to do that!
To make it easy to follow the rest of this tutorial you
should name you images like this: You make 5 images, name them norm1.gif norm2.gif and so
on. And 5 images that the norm image will change to when you mouseover, name them
over1.gif over2.gif and so on. Then place all the images in a folder named images. (you
can change this later if you wish)
function PreloadImages(length, path, type)
{
for(var i = 1; i<=length; i++) {
this[i]= new Image()
this[i].src= path + i + type
}
return this
}
|
The function used above will preload the images. We call it
with the line norm=new PreloadImages(5,'images/norm','.gif') .
"norm" is the name of the array we make, "5" is the number of pics we
have, "images/norm" is the path where the images is and the name of the image
and ".gif" is the imagetype. This will make the images be like this: norm[1].src='images/norm1.gif and so on. Now we have to
preload the over images. We do that using over=new
PreloadImages(5,'images/over','.gif') .
There is how to preload the images in a nutshell. If you
have more then five pictures or other names on the pictures just change it in those
calling lines.(example: you have 15 images, they are in the same folder as the html
document, and they are named hello1.jpg hello2.jpg and so on. The line would then be: hello=PreloadImages(15,'hello','.jpg') )
Here is the functions that will switch the images (we will
name the images in page later):
function rollOn(num){
if(document.images){
eval('document.images["norm'+num+'"].src
='+'over[num].src')}
}
|
That function is the one that changes the norm image to the
over image when you mouseover. Lets go line by line:
function rollOn(num){
- This line contains the name of the function and the argument num that we will pass when
we call the function.
if(document.images){
- This is the browsercheck. If the users browsers doesn't support the imagearray nothing
will happen in this function.
eval('document.images["norm'+num+'"].src
='+'over[num].src')} - This line uses the eval function to execute strings.
document.images -
The way to reach the image; the image is a image (obviously) and its on the document.
document.images -
The name of the image, I know we haven't named them yet but we will.
over[num].src -
The image that we placed in the src property of the over array earlier.
And here is the rollout function:
function rolloff(num){
if(document.images)
eval('document.images["norm'+num+'"].src='
+'norm[num].src')
}
|
This function does basically the same thing only the other
way around.
Now lets make the page that the script will run on:
<HTML>
<HEAD>
<TITLE>Mouseover</TITLE>
</HEAD>
<BODY>
<a href="#" onmouseover="rollOn(1)"
onmouseout="rolloff(1)">
<img src="../images/norm1.gif" name="norm1"
border="0"></a>
<a href="#" onmouseover="rollOn(2)"
onmouseout="rolloff(2)">
<img src="../images/norm2.gif" name="norm2"
border="0"></a>
<a href="#" onmouseover="rollOn(3)"
onmouseout="rolloff(3)">
<img src="../images/norm3.gif" name="norm3"
border="0"></a>
<a href="#" onmouseover="rollOn(4)"
onmouseout="rolloff(4)">
<img src="../images/norm4.gif" name="norm4"
border="0"></a>
<a href="#" onmouseover="rollOn(5)"
onmouseout="rolloff(5)">
<img src="../images/norm5.gif" name="norm5"
border="0"></a>
</BODY>
</HTML>
As you can see we name the images; norm1, norm2, norm3,
norm4 and norm5. And we call the rollOn function on mouseover and the rollOff function on
mouseout. The link point to # which really just means the top of the page.
Now we have to put in the finished script:
<script language="javascript">
<!--//Hiding from old browsers
function PreloadImages(length, path, type) {
for(var i = 1; i<=length; i++) {
this[i]= new Image()
this[i].src= path + i + type
}
return this
}
over=new PreloadImages(5,'../images/over','.gif')
norm=new PreloadImages(5,'../images/norm','.gif')
function rollOn(num){
if(document.images)
eval('document.images["norm'+num+'"].src='+'over[num].src')
}
function rolloff(num){
if(document.images)
eval('document.images["norm'+num+'"].src='
+'norm[num].src')
}
//-->end hiding
</script>
Just place this between the head tag in the page and
your set! |